package com.superman.supermarket.client.widget.payment;

import java.util.List;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.widget.toggle.ToggleList;
import com.superman.supermarket.client.widget.payment.type.AlipayPaymentModule;
import com.superman.supermarket.client.widget.payment.type.WXpayPaymentModule;

/**
 * 支付选择组件
 * 
 * @author superman
 * @version 2018年8月12日下午2:36:55
 */
public class PaymentChoose extends ToggleList {

	private FlowPanel flowPanel = new FlowPanel();

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午3:14:14
	 */
	public PaymentChoose() {

		FlowPanel mainpanel = new FlowPanel();
		mainpanel.setWidth("10rem");
		mainpanel.getElement().getStyle().setProperty("background", "#fff");
		mainpanel.add(initTitle());
		mainpanel.add(flowPanel);
		this.initWidget(mainpanel);
	}

	private Widget initTitle() {
		HTML titleHTML = new HTML("请选择支付方式");
		titleHTML.addStyleName("bd-b-1px ddd");
		titleHTML.getElement().getStyle().setProperty("lineHeight", "1.333333rem");
		titleHTML.getElement().getStyle().setProperty("fontSize", "13px");
		titleHTML.getElement().getStyle().setProperty("color", "#333");
		titleHTML.getElement().getStyle().setProperty("margin", "0rem 0.4rem");
		return titleHTML;
	}

	/**
	 * @author superman
	 * @version 2018年8月12日 下午2:45:03
	 * @param list
	 */
	public void setSupportPaymentList(List<String> list) {
		if (flowPanel.getWidgetCount() == 0) {
			for (int i = 0; i < list.size(); i++) {
				for (int j = 0; j < PaymentType.values().length; j++) {
					PaymentType payment = PaymentType.values()[j];
					if (payment.getType().equals(list.get(i))) {
						PaymentModule paymentModule = null;
						if (payment == PaymentType.ALIPAY) {
							paymentModule = new AlipayPaymentModule();
						} else if (payment == PaymentType.WXPAY) {
							paymentModule = new WXpayPaymentModule();
						}
						flowPanel.add(paymentModule);
						this.addItemToDict(paymentModule);
					}
				}
			}
			selectByIndex(0);
		}
	}

	// /**
	// * 获取选中的支付方式控件
	// *
	// * @author superman
	// * @version 2018年8月12日 下午2:41:11
	// * @return type
	// */
	// public String getSelectedPaymentType() {
	// Widget widget = this.getItem();
	// if (widget instanceof IPayment) {
	// IPayment payment = (IPayment) widget;
	// return payment.getType();
	// }
	// return null;
	// }
}
